import {mapState} from 'vuex';
export default{
	data(){
		return {
		}
	},
	computed:{
		...mapState('user',{
			userInfo: (state:any) => state.user
		}),
	},
	// beforeRouteLeave(to, from, next) {   
	// 	// 导航离开该组件的对应路由时调用
	// 	// 可以访问组件实例 `this`
	// 	uni.hideKeyboard()
	// 	this.$nextTick(()=>{
	// 		setTimeout(()=>{
	// 			next()
	// 		},200)
	// 	})
	// },
	mounted(){
	},
	methods: {
		hasInfo(){
			// console.log("mixinx.js----",this.userInfo,Boolean(Object.keys(this.userInfo).length > 0),this.userInfo.id)
			return   this.userInfo && Boolean(Object.keys(this.userInfo).length > 0);
		},
		/**
		 * 跳转再封装，不支持复杂传参。
		 * const openAnimation=['slide-in-right','slide-in-left','slide-in-top','slide-in-bottom','pop-in','fade-in','zoom-out','zoom-fade-out','none'];
		 * const closeAnimation=['slide-out-right','slide-out-left','slide-out-top','slide-out-bottom','pop-out','fade-out','zoom-in','zoom-fade-in','none'];
		 * animationDuration:500,
		 * animationType:openAnimation[~~(Math.random()*openAnimation.length-1)],
		 */
		navTo(path, params = {},opt={}) {
			// console.log("===this.userInfo",this.userInfo,uni.$store.state.user.user);
			let objParams = params;
			opt={type:'push',isCheckLogin:false ,...opt}; //opt传一值补全
			if(opt.isCheckLogin && !this.hasInfo()){
				uni.navigateTo({
					url:'/pages/login/login'
				})
				return;
			}
			// 是否跳转外部链接
			if (path.indexOf('http')>-1) {
				// #ifdef H5
				window.location = path;
				// #endif
				// #ifndef  H5
				uni.navigateTo({
					url:'/pages/public/webview?webviewPath='+path
				})
				// #endif
				return false
			}
			
			
			// 判断是否有参数
		// 	if (path.indexOf('?') !== -1) {
		// 		let index = path.lastIndexOf('?');
		// 		let query = path.substring(index + 1, path.length);
		// 		let arr = query.split('&')
		// 		path = path.slice(0, index);
		// 		arr.forEach(item => {
		// 			let mArr = []
		// 			let obj = {}
		// 			mArr = item.split('=');
		// 			obj[mArr[0]] = mArr[1];
		// 			objParams = {
		// 				...objParams,
		// 				...obj
		// 			}
		
		// 		})
		// 	}
		
			// let navObj={
			// 	push:'push',
			// 	replace:'replace',
			// 	replaceAll:'replaceAll',
			// 	pushTab:'pushTab',
			// }
			// uni[navObj[opt.type]]({
			// 	path: path,
			// 	query: objParams
			// })
			
			let navObj={
				push:'navigateTo',
				replace:'redirectTo',
				replaceAll:'reLaunch',
				pushTab:'switchTab',
			}
			let pathUrl=path;
			// console.log('navTo 2--')
			if(objParams && typeof objParams ==='object' && Object.keys(objParams).length > 0){
				let pathArr=[];
				Object.keys(objParams).forEach(k =>{
					pathArr.push(`${k}=${objParams[k]}`);
				})
				pathUrl = path +'?'+pathArr.join('&');
			}
			// console.log('---',navObj[opt.type],pathUrl)
			uni[navObj[opt.type]]({
				url:pathUrl
			})
		},
		async goBack(delta=1){
			// this.$Router.back(dela);
			// try{
				uni.navigateBack({
					delta,
					fail: (err) => {
						// console.log('err---',err)
						this.navTo('/pages/index/index',{},{type:'pushTab'})
					}
				});	
			// }catch(err){
			//    this.navTo('/pages/index/index',{},{type:'pushTab'})
			// }
		},
		stopPrevent(){},
	},
}